package org.opencv.SDK;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.nio.ByteBuffer;
import java.util.List;
import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.FastResultBean;
import org.opencv.core.Mat;

public class MatchingUtil {
    public static FastResultBean byByteBuffer(ByteBuffer byteBuffer, ByteBuffer byteBuffer2) {
        System.currentTimeMillis();
        Mat clone = new Mat(1080, 1920, CvType.CV_8UC3, byteBuffer).clone();
        Mat clone2 = new Mat(1080, 1920, CvType.CV_8UC3, byteBuffer2).clone();
        FastResultBean fastTemplateMatching = TemplateMatching.fastTemplateMatching(clone, clone2, 5, 0.1f, 0.2f, -1);
        if (fastTemplateMatching == null) {
            return null;
        }
        fastTemplateMatching.setWidth(clone2.cols());
        fastTemplateMatching.setHeight(clone2.rows());
        return fastTemplateMatching;
    }

    public static FastResultBean pyramidMatch(Bitmap bitmap, Bitmap bitmap2) {
        if (bitmap != null && bitmap2 != null && !bitmap.isRecycled() && !bitmap2.isRecycled()) {
            System.currentTimeMillis();
            Mat mat = new Mat();
            Utils.bitmapToMat(bitmap, mat);
            Mat mat2 = new Mat();
            Utils.bitmapToMat(bitmap2, mat2);
            FastResultBean fastTemplateMatching = TemplateMatching.fastTemplateMatching(mat, mat2, 5, 0.1f, 0.2f, -1);
            if (fastTemplateMatching != null) {
                fastTemplateMatching.setWidth(mat2.cols());
                fastTemplateMatching.setHeight(mat2.rows());
                return fastTemplateMatching;
            }
        }
        return null;
    }

    private static void recycle(Bitmap bitmap) {
        if (bitmap != null) {
            bitmap.recycle();
        }
    }

    private static Bitmap getBitmap(String str) {
        BitmapFactory.decodeFile(str);
        return BitmapFactory.decodeFile(str);
    }

    public static List<FastResultBean> pyramidMatchAll(Bitmap bitmap, Bitmap bitmap2, int i, int i2) {
        if (bitmap == null || bitmap2 == null || bitmap.isRecycled() || bitmap2.isRecycled()) {
            return null;
        }
        System.currentTimeMillis();
        Mat mat = new Mat();
        Utils.bitmapToMat(bitmap, mat);
        Mat mat2 = new Mat();
        Utils.bitmapToMat(bitmap2, mat2);
        List<FastResultBean> fastTemplateMatchingAll = TemplateMatchingAll.fastTemplateMatchingAll(mat, mat2, 5, i, i2);
        if (fastTemplateMatchingAll != null && fastTemplateMatchingAll.size() > 0) {
            for (FastResultBean fastResultBean : fastTemplateMatchingAll) {
                fastResultBean.setWidth(mat2.cols());
                fastResultBean.setHeight(mat2.rows());
            }
        }
        return fastTemplateMatchingAll;
    }
}
